package com.hgyc.mom.mes.websocket;

import com.hgyc.mom.mes.websocket.service.ConnectionManager;
import com.hgyc.mom.mes.websocket.service.MessageHandler;
import jakarta.websocket.*;
import jakarta.websocket.server.PathParam;
import jakarta.websocket.server.ServerEndpoint;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@ServerEndpoint("/ws/esop/document/{ip}")
public class WebSocketServer {

    private final ConnectionManager connectionManager = WebSocketContext.getBean(ConnectionManager.class);
    private final MessageHandler messageHandler = WebSocketContext.getBean(MessageHandler.class);

    @OnOpen
    public void onOpen(Session session, @PathParam("ip") String ip) {
        connectionManager.handleOpen(session, ip);
    }

    @OnClose
    public void onClose(Session session) {
        connectionManager.handleClose(session);
    }

    @OnError
    public void onError(Session session, Throwable throwable) {
        connectionManager.handleError(session, throwable);
    }

    @OnMessage
    public void onMessage(String message, Session session) {
        messageHandler.handleMessage(message, session);
    }
}
